package com.zy.core.common.security.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.Charset;

/**
 * Package: com.zy.core.common.security.redis
 * <p>
 * Description： TODO
 * <p>
 * Author: zhangyu
 * <p>
 * Date: Created in 2018-11-18 13:51
 * <p>
 * Copyright: Copyright (c) 2018
 * <p>
 * Version: 0.0.1
 * <p>
 * Modified By:
 */
public class FastJsonSerializeUtils {

    private static Logger logger = LoggerFactory.getLogger(FastJsonSerializeUtils.class);

    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    /**
     * 序列化
     * @param object
     * @return
     */
    public static byte[] serialize(Object object) {
        if (null == object) {
            return new byte[0];
        }
        logger.info("向redis中序列化对象" + object.toString());
        return JSON.toJSONString(object).getBytes(DEFAULT_CHARSET);
    }

    /**
     * 反序列化
     * @param bytes
     * @return
     */
    public static Object deserialize(byte[] bytes) {
        if (null == bytes || bytes.length <= 0) {
            return null;
        }
        String str = new String(bytes, DEFAULT_CHARSET);
        logger.info("向redis中反序列化[" + str + "]");
        return JSON.parseObject(str, Object.class);
    }
}
